﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Super_Star.Controllers
{
    /// <summary>
    /// 宿舍表控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class EMHostelController : ControllerBase
    {
        private readonly APIDBContext db;

        public EMHostelController(APIDBContext context)
        {
            db = context;
        }

        /// <summary>
        /// 查找宿舍人员信息信息
        /// </summary>
        /// <param name="StudentName"></param>
        /// <param name="Department"></param>
        /// <param name="Class"></param>
        /// <returns></returns>
        [HttpGet]
        public IQueryable<EM_Hostel> StudentEntity([FromHeader] string StudentName, [FromHeader] string Department, [FromHeader] string Class)
        {
            IQueryable<EM_Students> query = db.EM_Students;
            string a = "";
            a += "a";
            if (!string.IsNullOrEmpty(StudentName))
            {
                query = query.Where(s => s.StudentName == StudentName);
            }
            if (!string.IsNullOrEmpty(Department))
            {
                query = query.Where(s => s.Department == Department);
            }
            if (!string.IsNullOrEmpty(Class))
            {
                query = query.Where(s => s.Class == Class);
            }
            return (IQueryable<EM_Hostel>)query;
        }

        /// <summary>
        /// 添加宿舍人员信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost("UpHostel")]
        public bool UpHostel(Guid Id)
        {
            EM_Hostel? Hostel = db.EM_Hostel.Where(s => s.Id == Id).SingleOrDefault();
            if (Hostel != null)
            {
                db.EM_Hostel.Update(Hostel);
                int result = db.SaveChanges();
                return result > 0;
            }
            return false;
        }

        /// <summary>
        /// 删除宿舍人员信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelHostel")]
        public bool DelHostel(Guid Id)
        {
            EM_Hostel? Hostel = db.EM_Hostel.Where(s => s.Id == Id).SingleOrDefault();
            if (Hostel != null)
            {
                db.EM_Hostel.Remove(Hostel);
                int result = db.SaveChanges();
                return result > 0;
            }
            return false;
        }

    }
}
